package com.coolingme.calculate;

import com.coolingme.calculate.expression.TerminalExpression;
import com.coolingme.calculate.expression.nonterminal.AddExpression;
import com.coolingme.calculate.expression.nonterminal.DivideExpression;
import com.coolingme.calculate.expression.nonterminal.MultiplyExpression;
import com.coolingme.calculate.expression.nonterminal.SubTractExpression;

/**
 * 操作符工具类
 *
 * @author wangyue
 * @date 2020-09-29 22:51
 */
public class OperatorUtil {

    /**
     * 判断是否为操作符
     *
     * @param symbol 符号
     * @return boolean
     */
    public static boolean isOperator(String symbol) {
        return ("+".equals(symbol) || "-".equals(symbol) || "*".equals(symbol) || "/".equals(symbol));
    }

    /**
     * 获取表达式
     *
     * @param left   左边的表达式
     * @param symbol 操作符号
     * @param right  右边的表达式
     * @return TerminalExpression
     */
    public static TerminalExpression getExpression(IMathExpression left, String symbol, IMathExpression right) {
        TerminalExpression expression = null;
        switch (symbol) {
            case "+":
                expression = new AddExpression(left, right);
                break;
            case "-":
                expression = new SubTractExpression(left, right);
                break;
            case "*":
                expression = new MultiplyExpression(left, right);
                break;
            case "/":
                expression = new DivideExpression(left, right);
                break;
            default:
                System.err.println("无对应操作符的表达式");
        }
        return expression;
    }

}
